﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Core;
using Core.DataProvider;

namespace Core.DataProvider
{
    public partial class SqlConnector : IDisposable
    {
        private List<SqlParameter> spEntity;
        private List<SqlParameter> spEntityProperty;
        private List<SqlParameter> spAlert;

        private void InitDataConnector()
        {
            spEntity = new List<SqlParameter>();
            spEntity.Insert(0, new SqlParameter { ParameterName = "entity_type_id", IsNullable = false, DbType = DbType.Int32 });
            spEntity.Insert(1, new SqlParameter { ParameterName = "name", IsNullable = false, DbType = DbType.String });
            spEntity.Insert(2, new SqlParameter { ParameterName = "parent_id", IsNullable = true, DbType = DbType.Int32 });
            spEntity.Insert(3, new SqlParameter { ParameterName = "top_level_id", IsNullable = true, DbType = DbType.Int32 });

            spEntityProperty = new List<SqlParameter>();
            spEntityProperty.Insert(0, new SqlParameter { ParameterName = "entity_id", IsNullable = false, DbType = DbType.Int32 });
            spEntityProperty.Insert(1, new SqlParameter { ParameterName = "entity_type_property_id", IsNullable = false, DbType = DbType.Int32 });
            spEntityProperty.Insert(2, new SqlParameter { ParameterName = "string_value", IsNullable = false, DbType = DbType.String });
            spEntityProperty.Insert(3, new SqlParameter { ParameterName = "base_type", IsNullable = false, DbType = DbType.String });
            spEntityProperty.Insert(4, new SqlParameter { ParameterName = "int_value", IsNullable = true, DbType = DbType.Int32 });
            spEntityProperty.Insert(5, new SqlParameter { ParameterName = "bigint_value", IsNullable = true, DbType = DbType.Int64 });
            spEntityProperty.Insert(6, new SqlParameter { ParameterName = "datetime_value", IsNullable = true, DbType = DbType.DateTime });
            spEntityProperty.Insert(7, new SqlParameter { ParameterName = "double_value", IsNullable = true, DbType = DbType.Double });

            spAlert = new List<SqlParameter>();
            spAlert.Insert(0, new SqlParameter{ParameterName = "alert_rule_id", IsNullable = false, DbType = DbType.Int32});
            spAlert.Insert(1, new SqlParameter{ParameterName = "entity_id", IsNullable = false, DbType = DbType.Int32});
            spAlert.Insert(2, new SqlParameter{ParameterName = "entity_property_id", IsNullable = false, DbType = DbType.Int32});
        }


        public void AddAlert(AlertRule alertRule, Entity entity, EntityProperty entityProperty)
        {
            spAlert[0].Value = alertRule.Id;
            spAlert[1].Value = entity.Id;
            spAlert[2].Value = entityProperty.Id;

            ExecuteNonQuery("dbo.AddAlert", spAlert);
        }
        public int AddEntity(Entity entity)
        {
            int result = -1;             
            spEntity[0].Value = entity.EntityTypeId;
            spEntity[1].Value = entity.Name;
            spEntity[2].Value = (object)entity.ParentId ?? DBNull.Value;
            spEntity[3].Value = (object)entity.TopLevelId ?? DBNull.Value;

            result = Convert.ToInt32(ExecuteScalar("dbo.AddEntity", spEntity));

            Logging.LogInformationIf(result != -1, "Entity succesfully written to database...");

            foreach (SqlParameter p in spEntity)
            {
                Logging.LogInformationIf(Logging.IsCommandLine, string.Format("E  * {0}: {1}", p.ParameterName, p.Value.ToString()));
            }
            return result;
        }
        //public int AddEntity(Entity entity)
        //{
        //    int result = -1;
        //    List<SqlParameter> spEntity = new List<SqlParameter>();
        //    spEntity.Add(new SqlParameter { ParameterName = "entity_type_id", Value = entity.EntityTypeId, IsNullable = false, DbType = DbType.Int32 });
        //    spEntity.Add(new SqlParameter { ParameterName = "name", Value = entity.Name, IsNullable = false, DbType = DbType.String });
        //    spEntity.Add(new SqlParameter { ParameterName = "parent_id", Value = (object)entity.ParentId ?? DBNull.Value, IsNullable = true, DbType = DbType.Int32 });
        //    spEntity.Add(new SqlParameter { ParameterName = "top_level_id", Value = (object)entity.TopLevelId ?? DBNull.Value, IsNullable = true, DbType = DbType.Int32 });
        //    result = Convert.ToInt32(ExecuteScalar("dbo.AddEntity", spEntity));

        //    Logging.LogInformationIf(result != -1, "Entity succesfully written to database...");

        //    foreach (SqlParameter p in spEntity)
        //    {
        //        Logging.LogInformationIf(Logging.IsCommandLine, string.Format("E  * {0}: {1}", p.ParameterName, p.Value.ToString()));
        //    }
        //    return result;
        //}

        public int AddEntityProperty(EntityProperty entityProperty)
        {
            int result = -1;
            spEntityProperty[0].Value = entityProperty.Entity.Id;
            spEntityProperty[1].Value = entityProperty.EntityTypeProperty.Id;
            spEntityProperty[2].Value = entityProperty.StringValue;
            spEntityProperty[3].Value = entityProperty.BaseType;
            spEntityProperty[4].Value = (object)entityProperty.IntValue ?? DBNull.Value;
            spEntityProperty[5].Value = (object)entityProperty.BigIntValue ?? DBNull.Value;
            spEntityProperty[6].Value = (object)entityProperty.DateTimeValue ?? DBNull.Value;
            spEntityProperty[7].Value = (object)entityProperty.DoubleValue ?? DBNull.Value;
            result = Convert.ToInt32(ExecuteScalar("dbo.AddEntityProperty", spEntityProperty));

            foreach (SqlParameter p in spEntityProperty)
            {
                Logging.LogInformationIf(Logging.IsCommandLine, string.Format("EP * {0}: {1}", p.ParameterName, p.Value.ToString()));
            }
            return result;
        }

        //public void AddEntityProperty(EntityProperty entityProperty)
        //{
        //    List<SqlParameter> spEntityProperty = new List<SqlParameter>();
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "entity_id", Value = entityProperty.Entity.Id, IsNullable = false, DbType = DbType.Int32 });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "entity_type_property_id", Value = entityProperty.EntityTypeProperty.Id, IsNullable = false, DbType = DbType.Int32 });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "string_value", Value = entityProperty.StringValue, IsNullable = false, DbType = DbType.String });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "base_type", Value = entityProperty.BaseType, IsNullable = false, DbType = DbType.String });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "int_value", Value = (object)entityProperty.IntValue ?? DBNull.Value, IsNullable = true, DbType = DbType.Int32 });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "bigint_value", Value = (object)entityProperty.BigIntValue ?? DBNull.Value, IsNullable = true, DbType = DbType.Int64 });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "datetime_value", Value = (object)entityProperty.DateTimeValue ?? DBNull.Value, IsNullable = true, DbType = DbType.DateTime });
        //    spEntityProperty.Add(new SqlParameter { ParameterName = "double_value", Value = (object)entityProperty.DoubleValue ?? DBNull.Value, IsNullable = true, DbType = DbType.Double });
        //    ExecuteNonQuery("dbo.AddEntityProperty", spEntityProperty);

        //    foreach (SqlParameter p in spEntityProperty)
        //    {
        //        Logging.LogInformationIf(Logging.IsCommandLine, string.Format("EP * {0}: {1}", p.ParameterName, p.Value.ToString()));
        //    }
        //}
    }
}
